<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据号" prop="serialNo">
        <el-input
          v-model="queryParams.serialNo"
          placeholder="请输入单据号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调度单号" prop="dispatchBillNo">
        <el-input
          v-model="queryParams.dispatchBillNo"
          placeholder="请输入调度单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="plateNo">
        <el-input
          v-model="queryParams.plateNo"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配送状态" prop="billStatus">
        <el-select v-model="queryParams.billStatus" placeholder="请选择配送状态" clearable>
          <el-option
            v-for="dict in delivery_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户" prop="customerId">
        <el-select v-model="queryParams.customerId" placeholder="请选择客户" clearable>
          <el-option
            v-for="dict in sys_com_query"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['transportApply:deliveryBill:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['transportApply:deliveryBill:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['transportApply:deliveryBill:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['transportApply:deliveryBill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deliveryBillList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="单据号" align="center" prop="serialNo" />
      <el-table-column label="调度单号" align="center" prop="dispatchBillNo" />
      <el-table-column label="车牌号" align="center" prop="plateNo" />
      <el-table-column label="司机" align="center" prop="driver" />
      <el-table-column label="司机联系电话" align="center" prop="contactTel" />
      <el-table-column label="配送状态" align="center" prop="billStatus">
        <template #default="scope">
          <dict-tag :options="delivery_status" :value="scope.row.billStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="配送时间" align="center" prop="deliveryTime" />
      <el-table-column label="到达时间" align="center" prop="arriveTime" />
      <el-table-column label="客户" align="center" prop="customerId">
        <template #default="scope">
          <dict-tag :options="sys_com_query" :value="scope.row.customerId"/>
        </template>
      </el-table-column>
      <el-table-column label="客户联系人" align="center" prop="customerContact" />
      <el-table-column label="客户联系电话" align="center" prop="customerContactTel" />
      <el-table-column label="配送地址" align="center" prop="deliveryAddress" />
      <el-table-column label="发货地址" align="center" prop="shipAddress" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['transportApply:deliveryBill:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['transportApply:deliveryBill:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改配送管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="deliveryBillRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单据号" prop="serialNo">
          <el-input v-model="form.serialNo" placeholder="请输入单据号" />
        </el-form-item>
        <el-form-item label="调度id" prop="dispatchBillId">
          <el-input v-model="form.dispatchBillId" placeholder="请输入调度id" />
        </el-form-item>
        <el-form-item label="调度单号" prop="dispatchBillNo">
          <el-input v-model="form.dispatchBillNo" placeholder="请输入调度单号" />
        </el-form-item>
        <el-form-item label="车牌号" prop="plateNo">
          <el-input v-model="form.plateNo" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="司机" prop="driver">
          <el-input v-model="form.driver" placeholder="请输入司机" />
        </el-form-item>
        <el-form-item label="司机联系电话" prop="contactTel">
          <el-input v-model="form.contactTel" placeholder="请输入司机联系电话" />
        </el-form-item>
        <el-form-item label="配送状态" prop="billStatus">
          <el-select v-model="form.billStatus" placeholder="请选择配送状态">
            <el-option
              v-for="dict in delivery_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户" prop="customerId">
          <el-select v-model="form.customerId" placeholder="请选择客户">
            <el-option
              v-for="dict in sys_com_query"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户联系人" prop="customerContact">
          <el-input v-model="form.customerContact" placeholder="请输入客户联系人" />
        </el-form-item>
        <el-form-item label="客户联系电话" prop="customerContactTel">
          <el-input v-model="form.customerContactTel" placeholder="请输入客户联系电话" />
        </el-form-item>
        <el-form-item label="配送地址" prop="deliveryAddress">
          <el-input v-model="form.deliveryAddress" placeholder="请输入配送地址" />
        </el-form-item>
        <el-form-item label="发货地址" prop="shipAddress">
          <el-input v-model="form.shipAddress" placeholder="请输入发货地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-divider content-position="center">配送单明细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddWmsDeliveryBillDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteWmsDeliveryBillDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="wmsDeliveryBillDetailList" :row-class-name="rowWmsDeliveryBillDetailIndex" @selection-change="handleWmsDeliveryBillDetailSelectionChange" ref="wmsDeliveryBillDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="物料" prop="materialId" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.materialId" placeholder="请输入物料" />
            </template>
          </el-table-column>
          <el-table-column label="生产批号" prop="batchNo" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.batchNo" placeholder="请输入生产批号" />
            </template>
          </el-table-column>
          <el-table-column label="仓库" prop="warehouseId" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.warehouseId" placeholder="请选择仓库">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="库区" prop="whRegionId" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.whRegionId" placeholder="请选择库区">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="库位" prop="whLocationId" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.whLocationId" placeholder="请选择库位">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="发货数量" prop="deliveryQuantity" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.deliveryQuantity" placeholder="请输入发货数量" />
            </template>
          </el-table-column>
          <el-table-column label="价格" prop="price" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.price" placeholder="请输入价格" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="DeliveryBill">
import { listDeliveryBill, getDeliveryBill, delDeliveryBill, addDeliveryBill, updateDeliveryBill } from "@/api/transportApply/deliveryBill";

const { proxy } = getCurrentInstance();
const { delivery_status, sys_com_query } = proxy.useDict('delivery_status', 'sys_com_query');

const deliveryBillList = ref([]);
const wmsDeliveryBillDetailList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedWmsDeliveryBillDetail = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    serialNo: null,
    dispatchBillNo: null,
    plateNo: null,
    billStatus: null,
    deliveryTime: null,
    arriveTime: null,
    customerId: null,
  },
  rules: {
    deliveryTime: [
      { required: true, message: "配送时间不能为空", trigger: "blur" }
    ],
    deliveryAddress: [
      { required: true, message: "配送地址不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询配送管理列表 */
function getList() {
  loading.value = true;
  listDeliveryBill(queryParams.value).then(response => {
    deliveryBillList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    serialNo: null,
    dispatchBillId: null,
    dispatchBillNo: null,
    plateNo: null,
    driver: null,
    contactTel: null,
    billStatus: null,
    deliveryTime: null,
    arriveTime: null,
    customerId: null,
    customerContact: null,
    customerContactTel: null,
    deliveryAddress: null,
    shipAddress: null,
    remark: null
  };
  wmsDeliveryBillDetailList.value = [];
  proxy.resetForm("deliveryBillRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加配送管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getDeliveryBill(_id).then(response => {
    form.value = response.data;
    wmsDeliveryBillDetailList.value = response.data.wmsDeliveryBillDetailList;
    open.value = true;
    title.value = "修改配送管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["deliveryBillRef"].validate(valid => {
    if (valid) {
      form.value.wmsDeliveryBillDetailList = wmsDeliveryBillDetailList.value;
      if (form.value.id != null) {
        updateDeliveryBill(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDeliveryBill(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除配送管理编号为"' + _ids + '"的数据项？').then(function() {
    return delDeliveryBill(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 配送单明细序号 */
function rowWmsDeliveryBillDetailIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 配送单明细添加按钮操作 */
function handleAddWmsDeliveryBillDetail() {
  let obj = {};
  obj.materialId = "";
  obj.batchNo = "";
  obj.warehouseId = "";
  obj.whRegionId = "";
  obj.whLocationId = "";
  obj.deliveryQuantity = "";
  obj.price = "";
  obj.detailRemark = "";
  wmsDeliveryBillDetailList.value.push(obj);
}

/** 配送单明细删除按钮操作 */
function handleDeleteWmsDeliveryBillDetail() {
  if (checkedWmsDeliveryBillDetail.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的配送单明细数据");
  } else {
    const wmsDeliveryBillDetails = wmsDeliveryBillDetailList.value;
    const checkedWmsDeliveryBillDetails = checkedWmsDeliveryBillDetail.value;
    wmsDeliveryBillDetailList.value = wmsDeliveryBillDetails.filter(function(item) {
      return checkedWmsDeliveryBillDetails.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleWmsDeliveryBillDetailSelectionChange(selection) {
  checkedWmsDeliveryBillDetail.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('transportApply/deliveryBill/export', {
    ...queryParams.value
  }, `deliveryBill_${new Date().getTime()}.xlsx`)
}

getList();
</script>
